<?php
require_once 'Htmlpurifier/HTMLPurifier.includes.php';
require_once 'Htmlpurifier/HTMLPurifier.autoload.php';

class YouNet_Filter_Purifier implements Zend_Filter_Interface
{
    protected $_htmlPurifier = null;

    public function __construct($options = null)
    {
        // set up configuration
        $config = HTMLPurifier_Config::createDefault();        
        $config->set('Core.Encoding', 'utf-8'); 
        $config->set('HTML.Doctype', 'XHTML 1.0 Transitional');

        // configure caching
        $cachePath = CACHE_PATH . '/htmlpurifier';
        if (!is_dir($cachePath)) {
            mkdir($cachePath, 0755, true);
        }
        $cachePath = realpath($cachePath);
        $config->set('Cache.SerializerPath', $cachePath);

        if (!is_null($options)) {
            //$config = HTMLPurifier_Config::createDefault();
            foreach ($options as $option) {
                $config->set($option[0], $option[1], $option[2]);
            }
        }
		
		$def = $config->getHTMLDefinition(true);
		/* @var $def HTMLPurifier_HTMLDefinition */
		$def->addAttribute('a', 'target', new HTMLPurifier_AttrDef_Enum(
			array('_blank','_self')
		));
		
        $this->_htmlPurifier = new HTMLPurifier($config);
    }

    public function filter($value)
    {
        return $this->_htmlPurifier->purify($value);
    }
}
